package com.feihong.yw189.dialog;


import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.feihong.yw189.R;
import com.feihong.yw189.bluetooth.utils.util.PrefUtils;
import com.feihong.yw189.model.User.UserEntity;
import com.feihong.yw189.utils.MessageEvent;
import com.feihong.yw189.utils.UserLoginInfo;

import org.greenrobot.eventbus.EventBus;

import permissions.dispatcher.RuntimePermissions;

/**
 * 暂时不用了,写到fragment中
 * Created by PVer on 2018/1/15.
 */
public class ServiceDialogFragment extends Dialog implements View.OnClickListener {
    private static final String TAG = "营维189";

    private FragmentActivity mActivity;
    private TextView manager;
    private TextView source;
    private TextView service;
    private TextView btn;
    private String mContaceTel;
    private String mAgentTel;
    private String mPlatFormTel;

    public ServiceDialogFragment(FragmentActivity mActivity) {
        super(mActivity, R.style.MyDefindDialog);
        this.mActivity = mActivity;
        initView();
    }

    public void initView() {
        setContentView(R.layout.activity_service_phone);
        manager = (TextView) findViewById(R.id.my_service_manager);
        source = (TextView) findViewById(R.id.service_super_source);
        service = (TextView) findViewById(R.id.service_phone);
        btn = (TextView) findViewById(R.id.service_cancel);
        UserEntity userEntity = UserLoginInfo.getUserLoginInfoInstance().getUserLoc();
        mContaceTel = userEntity.getUserAccountManagerContactTel();
        mAgentTel = userEntity.getUserAgentTel();
        mPlatFormTel = userEntity.getPlatFormTel();

        manager.setText(mContaceTel); // 客户经理电话
        source.setText(mAgentTel); // 上级渠道经理电话
        service.setText(mPlatFormTel); // 平台电话

        findViewById(R.id.my_service_manager_btn).setOnClickListener(this);
        findViewById(R.id.service_super_source_btn).setOnClickListener(this);
        findViewById(R.id.service_phone_btn).setOnClickListener(this);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cancel();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.my_service_manager_btn:
                if(!TextUtils.isEmpty(mContaceTel)){
                    call(""+ manager.getText());
                }
                break;
            case R.id.service_super_source_btn:
                if(!TextUtils.isEmpty(mAgentTel)){
                    call(""+ source.getText());
                }
                break;
            case R.id.service_phone_btn:
                if(!TextUtils.isEmpty(mPlatFormTel)) {
                    call("" + service.getText());
                }
                break;
        }
    }

    private void call(String phone){
        EventBus.getDefault().postSticky(new MessageEvent(MessageEvent.CALL_PER));
        if (ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
//            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
            try{
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                this.mActivity.startActivity(intent);
            }catch (Exception e){}
        }
    }

    @Override
    public void cancel() {
        super.cancel();
    }

    public void showDialog() {
        show();
        WindowManager windowManager = mActivity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int) (display.getWidth()); //设置宽度
        getWindow().setAttributes(lp);
    }

}